package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.R;
import com.sgb.kjwl.utils.TabLayoutUtils;
import com.sgb.kjwl.view.ui.activity.enterprise.MakeOverActivity;

import java.util.List;


public class ActionBarTab extends RelativeLayout {

    ImageView iconFontBack;
    TextView fontIconLdrtl;
    private ImageView fivContactSearch;
    private SelectTab mSelectTab;
    private RelativeLayout relativeLayout;
    private View view;
    private TabLayout mTabLayout;
    private boolean isShowcenter = false;

    public void setSelectTab(SelectTab clearEdiText) {
        this.mSelectTab = clearEdiText;
    }

    public SelectTab getmSelectTab() {
        return mSelectTab;
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
    }

    public ActionBarTab(Context context) {
        super(context);
        init(context, null);
    }

    public ActionBarTab(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ActionBarTab(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {

            TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.ActionBarTabYoung, 0, 0);
            try {
                isShowcenter = attributes.getBoolean(R.styleable.ActionBarTabYoung_tabisGravityCenter,false);

                LayoutInflater.from(context).inflate(isShowcenter?R.layout.action_bar_tab_center:R.layout.action_bar_tab, this);

                //回退按钮
                iconFontBack = findViewById(R.id.iv_back);
                //actionbar的标题
                fivContactSearch = findViewById(R.id.map_view);
                //右边的文字
                fontIconLdrtl = findViewById(R.id.right_view);
                int txtColor = attributes.getColor(R.styleable.ActionBarTabYoung_txtColorTab, 0);

                boolean isVisibleMenu = attributes.getBoolean(R.styleable.ActionBarTabYoung_isVisibleMenuTab, false);
                if (isVisibleMenu) {
                    fivContactSearch.setVisibility(VISIBLE);
                } else {
                    fivContactSearch.setVisibility(GONE);
                }

                String menuFont = attributes.getString(R.styleable.ActionBarTabYoung_menuTextTab);
                if (menuFont == null || menuFont.equals("")) {
                    menuFont = "保存";
                }
                fontIconLdrtl.setText(menuFont);
                fontIconLdrtl.setTextColor(txtColor);

                boolean isVisibleMenuFunction = attributes.getBoolean(R.styleable.ActionBarTabYoung_isVisibleMenuFunctionTab, false);
                setMenuFunctionVisible(isVisibleMenuFunction);

                relativeLayout = findViewById(R.id.rl_title);
                view = findViewById(R.id.liuhai_status_view);
                int backgroundColor = attributes.getColor(R.styleable.ActionBarTabYoung_backColorTab, 0);
                if (backgroundColor != 0) {
                   relativeLayout.setBackgroundColor(backgroundColor);
                   view.setBackgroundColor(backgroundColor);
                }

                mTabLayout = findViewById(R.id.tab_action_bar);
                int resource = attributes.getResourceId(R.styleable.ActionBarTabYoung_actionbarTabView,0);
                if (resource != 0){
                    View linearLayout = LayoutInflater.from(getContext()).inflate(resource, null);
                    TextView selectTextView = linearLayout.findViewById(R.id.tab_item_textview);

                    mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                        @Override
                        public void onTabSelected(TabLayout.Tab tab) {
                            selectTextView.setText(tab.getText());
                            tab.setCustomView(linearLayout);
                            if (mSelectTab != null){
                                mSelectTab.selectTab(tab);
                            }
                        }

                        @Override
                        public void onTabUnselected(TabLayout.Tab tab) {
                            tab.setCustomView(null);
                            if (mSelectTab != null){
                                mSelectTab.unSelectTab(tab);
                            }
                        }

                        @Override
                        public void onTabReselected(TabLayout.Tab tab) {

                        }
                    });
                }else {
                    mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                        @Override
                        public void onTabSelected(TabLayout.Tab tab) {
                            if (mSelectTab != null){
                                mSelectTab.selectTab(tab);
                            }
                        }

                        @Override
                        public void onTabUnselected(TabLayout.Tab tab) {
                            if (mSelectTab != null){
                                mSelectTab.unSelectTab(tab);
                            }
                        }

                        @Override
                        public void onTabReselected(TabLayout.Tab tab) {

                        }
                    });
                }

//                float indicator = attributes.getDimension(R.styleable.ActionBarTabYoung_tabIndicatorWidth,0);
//                if (indicator != 0){
//                    mTabLayout.setSelectedTabIndicatorColor(Color.rgb(255,255,255));
//                    TabLayoutUtils.reflex(mTabLayout,indicator);
//                }
            } finally {
                attributes.recycle();
            }
        }
    }

    public void setTitleMenuImg(int originLang) {
        iconFontBack.setImageResource(originLang);
    }

    public void setOnBackClickListener(OnClickListener l) {
        iconFontBack.setOnClickListener(l);
    }

    public void setMenuClick(OnClickListener click) {
        fivContactSearch.setOnClickListener(click);
    }

    public void setMenuIcon(@DrawableRes int drawable) {
        fivContactSearch.setImageResource(drawable);
    }

    public void setMenuVisible(boolean flag) {
        if (flag) {
            fivContactSearch.setVisibility(VISIBLE);
        } else {
            fivContactSearch.setVisibility(GONE);
        }
    }

    public void setMenuFunctionClick(OnClickListener click) {
        fontIconLdrtl.setOnClickListener(click);
    }

    public void setMenuFunctionVisible(boolean isVisibleMenuFunction) {
        if (isVisibleMenuFunction) {
            fontIconLdrtl.setVisibility(VISIBLE);
        } else {
            fontIconLdrtl.setVisibility(GONE);
        }
    }

    public void setMenuFunctionText(String isVisibleMenuFunction) {
        fontIconLdrtl.setText(isVisibleMenuFunction);
    }

    public String getMenuFunctionText() {
        return fontIconLdrtl.getText().toString();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    public interface SelectTab{
        default void unSelectTab(TabLayout.Tab tab) {}
        void selectTab(TabLayout.Tab tab);
    }

    public void setTab(String...content){
        for (int i = 0; i < content.length; i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(content[i]));
        }
    }

    public void setTabForArray(String[] content){
        for (int i = 0; i < content.length; i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(content[i]));
        }
    }

    public void setTabForList(List<String> content){
        for (int i = 0; i < content.size(); i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(content.get(i)));
        }
    }

    public void setTabSelet(int index){
        if(mTabLayout.getTabCount()>index){
            mTabLayout.postDelayed(()->{
                mTabLayout.getTabAt(index).select();
            },100);
        }
    }

    public void setViewPagerUp(ViewPager view){
        mTabLayout.setupWithViewPager(view);
    }

    public int getTabPosition(){
        return mTabLayout.getSelectedTabPosition();
    }
}
